<template>
  <div class="settings-form">
    <DynamicForm ref="dynamicFormRef" :rulesForm="state.settings" @click="callBack" :refresh="state.refresh"/>
    <!--    <DynamicTable ref="dynamicTableRef"/>-->
  </div>
</template>

<script lang="ts" setup>
import {defineEmits, defineProps, onMounted, reactive, ref} from 'vue';
import DynamicTable from '@/components/layout/table/dynamic-table.vue';
import DynamicForm from '@/components/layout/form/dynamic-form.vue';
import {settingForms} from '@/assets/script/monitor/forms/settings/add-settings';
import {buildEarlyRuleApi, findEarlyRuleApi} from '@/api/interfaces/early';
import {ElMessage} from "element-plus";

const dynamicTableRef = ref(DynamicTable)
const dynamicFormRef = ref(DynamicForm)

const state = reactive({
  settings: settingForms as any,
  refresh: 0 as number
})

onMounted(() => {
  findEarlyRule()
})

const callBack = (data: any) => {
  switch (data.type || '') {
    case 'submit':
      buildEarlyRule(data.data)
      break
    default:
      console.log('callBack', data)
      break
  }
}

const findEarlyRule = (params = {} as any) => {
  const card = state.settings[0].card
  findEarlyRuleApi(params).then((data) => {
    card.forEach((e: any) => {
      if (data[e.name]) {
        e[e.name] = data[e.name]
      }
    })
    state.refresh = Math.random()
  })
}

const buildEarlyRule = (params = {} as any) => {
  buildEarlyRuleApi(params).then((data) => {
    if (data.code === 0) {
      ElMessage.success('保存成功！')
    } else {
      ElMessage.warning('保存失败！')
    }
  })
}

</script>